package com.yh.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yh.dao.IEmpDao;
import com.yh.model.Emp;
import com.yh.model.Page;

@Controller("empAction")
@Scope("prototype")
public class EmpAction extends ActionSupport implements ModelDriven {
	
	@Autowired
	private Emp emp;
	@Autowired
	private Page page;
	@Autowired
	private IEmpDao  edi;
	
	private int pageIndex;  //用来获取页面传过来的当前页码参数
	private int pageSize;   //用来获取页面传过来的当前显示多少条记录
	
	
	public Emp getEmp() {
		return emp;
	}

	public void setEmp(Emp emp) {
		this.emp = emp;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String execute(){
		return "success";
	}
	public Object getModel() {
		return emp;
	}
	
	
	
	/**
	 * 异步显示EmpByPage
	 * @throws UnsupportedEncodingException 
	 */
	public String showEmp() throws UnsupportedEncodingException{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("utf-8");
		
		int pageCount=edi.selectAll().size(); //总条数
		page.setPageSize(pageSize);
		page.setPageCount(pageCount);
		page.setPageIndex(pageIndex);
		Map map=new HashMap();
		map.put("startRow", (pageIndex - 1) * pageSize);
		map.put("rowSize", pageSize);
		page.setPageList(edi.selectByPage(map));
		 return "success";
	}
	
	public void showEmpByPage() throws UnsupportedEncodingException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		PrintWriter out=null;
		response.setCharacterEncoding("UTF-8");  
		request.setCharacterEncoding("utf-8");
		
		String index = request.getParameter("index");
		//System.out.println(index);
		try {
			out= response.getWriter();
		
			int pageIndex=1;
			int pageSize=3;
			int pageCount=edi.selectAll().size(); //�ܼ�¼��
			page.setPageSize(pageSize);
			page.setPageCount(pageCount);
			
			if(index!=null){
				pageIndex=Integer.parseInt(index);
				if(pageIndex<=0){
					pageIndex=1;
				}else if(pageIndex>=page.getPageTotal()){
					pageIndex=page.getPageTotal();
				}
			}
			page.setPageIndex(pageIndex);
			Map map=new HashMap();
			map.put("startRow", (pageIndex - 1) * pageSize);
			map.put("rowSize", pageSize);
			page.setPageList(edi.selectByPage(map));
			//request.setAttribute("page", page);
			out.print(JSON.toJSONString(page));
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.flush();
			out.close();
		}
		
	}
	
	
	public void getPages() throws UnsupportedEncodingException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out=null;
		response.setCharacterEncoding("UTF-8");  
		try {
			out= response.getWriter();
			int pageSize=3;
			int pageCount=edi.selectAll().size(); //�ܼ�¼��
			page.setPageSize(pageSize);
			page.setPageCount(pageCount);
			int pageTotal=page.getPageTotal();
			out.print(pageTotal);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			out.flush();
			out.close();
		}
		
	}
	
	/**
	 * �����û�
	 * @return
	 */
	public String addEmp(){
		int i=0;
		//System.out.println(emp.getEmpName());
		i=edi.insertEmp(emp);
		if(i>0){
			return "success";
		}
		return "fail";
	}
	

	/**
	 * �޸��û�
	 * @return
	 */
	public String updateEmp(){
		int i=0;
		i=edi.updateEmp(emp);
		if(i>0){
			return "success";
		}
		return "fail";
	}
	/**
	 * ���ݸ��´������Ĳ���,�õ�emp����,�û���ҳ�����
	 * @return
	 */
	public String updateEmp1(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String empno = request.getParameter("empno");
		//System.out.println(empno);
		emp = edi.selectOne(Integer.parseInt(empno));
		request.setAttribute("emp", emp);
		return "success";
	}

	
	public void delEmp() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");  
		request.setCharacterEncoding("utf-8");
		PrintWriter out= response.getWriter();
		String empno = request.getParameter("empno");
		String index = request.getParameter("index");
		//System.out.println(index);
		int i=0;
		i= edi.deleteEmp(Integer.parseInt(empno));
		if(i>0){
			
			int pageIndex=1;
			int pageSize=3;
			int pageCount=edi.selectAll().size(); //�ܼ�¼��
			page.setPageSize(pageSize);
			page.setPageCount(pageCount);
			
			if(index!=null){
				pageIndex=Integer.parseInt(index);
				if(pageIndex<=0){
					pageIndex=1;
				}else if(pageIndex>=page.getPageTotal()){
					pageIndex=page.getPageTotal();
				}
			}
			page.setPageIndex(pageIndex);
			Map map=new HashMap();
			map.put("startRow", (pageIndex - 1) * pageSize);
			map.put("rowSize", pageSize);
			page.setPageList(edi.selectByPage(map));
			//request.setAttribute("page", page);
			out.print(JSON.toJSONString(page));
		}else{
			out.print("{\"cdata\":\"0\",\"ctype\":\"ɾ��ʧ��\"}");
		}
		
		
		out.flush();
		out.close();
	}
	
	
	
}
